package com.example.joewang.repluginmotion.hook;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.joewang.repluginmotion.plugin.ReflectionUtils;


public class HandlerHook implements android.os.Handler.Callback {

    public static final int LAUNCH_ACTIVITY = 100;
    private Handler mOriginHandler;

    public HandlerHook(Handler handler) {
        mOriginHandler = handler;
    }

    @Override
    public boolean handleMessage(Message msg) {
        if (msg.what == LAUNCH_ACTIVITY) {
            Object obj = msg.obj;
            Intent intent = (Intent) ReflectionUtils.getFieldObject(obj.getClass(), "intent", obj);
            Intent originIntent = intent.getParcelableExtra(HookUtils.HOOK_ORIGIN_INTENT);
            if (originIntent != null) {
                intent.setComponent(originIntent.getComponent());
            }
        }
        mOriginHandler.handleMessage(msg);
        return true;
    }
}
